#wd = '/home/jc165798/Climate/CIAS/Australia/5km/bioclim_asc/'; setwd(wd) #define and set the raw data directory
wd = '/home/jc165798/Climate/CIAS/Australia/1km/bioclim_asc/'; setwd(wd)
od = '/home/jc148322/clim.data/Australia_1km/'; dir.create(od) #define and create the output directory


tfiles = list.files() #get a list of the files
tt = strsplit(tfiles,'_') #string split the file names
ESs = GCMs = NULL #set all to null
for (tval in tt) { ESs = c(ESs,tval[1]); GCMs = c(GCMs,tval[2]); } #extract all possible values
ESs = unique(ESs); GCMs = na.omit(unique(GCMs)) #keep only unique


###write out each emission scenario
for (es in ESs) {
zip(paste(od,es,'.zip',sep=''),tfiles[grep(es,tfiles)],flags = "-r0X")
}
###write out each GCM
for (gcm in GCMs) {
zip(paste(od,'RCP_',gcm,'.zip',sep=''),tfiles[intersect(grep(gcm,tfiles),grep('RCP',tfiles))],flags = "-r0X")
zip(paste(od,'SRES_',gcm,'.zip',sep=''),tfiles[intersect(grep(gcm,tfiles),grep('SRES',tfiles))],flags = "-r0X")
}
###write out the combination of gcm & es
for (es in ESs) {
for (gcm in GCMs) {
zip(paste(od,es,'_',gcm,'.zip',sep=''),tfiles[intersect(grep(gcm,tfiles),grep(es,tfiles))],flags = "-r0X")
}
}


###write out the current
# zip(paste(od,'current.zip',sep=''),current,flags = "-r0X")

# current = ESs[grep('current',ESs)] #get the current asciis
# ESs = ESs[-grep('current',ESs)] #remove current from ESs

